package com.nq.stock.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nq.common.Result;
import com.nq.common.entity.StStockTickerDetails;
import com.nq.common.entity.StStockTickerKLineList;
import com.nq.common.entity.info.StockTickerListInfo;
import com.nq.common.entity.vo.StStockTickerDetailsVo;
import com.nq.stock.entity.StStockTicker;
import com.nq.stock.service.IStStockTickerService;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nq.stock.vo.StStockTickerKLineVO;
import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;

 /**
 * @Description: st_stock_ticker
 * @Author: jeecg-boot
 * @Date:   2024-05-30
 * @Version: V1.0
 */
@RestController
@RequestMapping("/api/stStockTicker")
@Slf4j
public class StStockTickerController{
	@Autowired
	private IStStockTickerService stStockTickerService;
	
	/**
	 * 分页列表查询
	 *
	 * @param stStockTicker
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@GetMapping(value = "/list")
	public Result<?> queryPageList(StStockTicker stStockTicker,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<StStockTicker> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq(StringUtils.isNotBlank(stStockTicker.getCode()), "code", stStockTicker.getCode());
		queryWrapper.orderByDesc("up");
		Page<StStockTicker> page = new Page<StStockTicker>(pageNo, pageSize);
		IPage<StStockTicker> pageList = stStockTickerService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	 /**
	  * 通过市场和编码查询详情
	  *
	  * @param market
	  * @param code
	  * @return
	  */
	 @GetMapping(value = "/queryDetails")
	 public Result<?> queryDetails(@RequestParam(name="market",required=true) String market, @RequestParam(name="code",required=true) String code) {
		 StStockTickerDetailsVo stStockTicker = stStockTickerService.queryDetails(market, code);
		 if(stStockTicker==null) {
			 return Result.error("No corresponding data found");
		 }
		 return Result.OK(stStockTicker);
	 }

	 /**
	  * 通过市场和编码查询K线详情
	  *
	  * @param market
	  * @param code
	  * @return
	  */
	 @GetMapping(value = "/queryKLineDetails")
	 public Result<?> queryKLineDetails(@RequestParam(name="market", required=false) String market,
										@RequestParam(name="code", required=false) String code,
										@RequestParam(name="interval", required=false) String interval,
										@RequestParam(name="page", required=false) String page) {
		 StStockTickerKLineVO kLineList = stStockTickerService.queryKLineDetails(market, code, interval, page);
		 if(kLineList==null) {
			 return Result.error("No corresponding data found");
		 }
		 return Result.OK(kLineList);
	 }

	/**
	 *   添加
	 *
	 * @param stStockTicker
	 * @return
	 */
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody StStockTicker stStockTicker) {
		stStockTickerService.save(stStockTicker);
		return Result.OK("Added successfully！");
	}
	
	/**
	 *  编辑
	 *
	 * @param stStockTicker
	 * @return
	 */
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody StStockTicker stStockTicker) {
		stStockTickerService.updateById(stStockTicker);
		return Result.OK("Editing successfully!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		stStockTickerService.removeById(id);
		return Result.OK("Deleted successfully!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.stStockTickerService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("Batch Delete Successfully!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		StStockTicker stStockTicker = stStockTickerService.getById(id);
		if(stStockTicker==null) {
			return Result.error("No corresponding data found");
		}
		return Result.OK(stStockTicker);
	}

//    /**
//    * 导出excel
//    *
//    * @param request
//    * @param stStockTicker
//    */
//    @RequestMapping(value = "/exportXls")
//    public ModelAndView exportXls(HttpServletRequest request, StStockTicker stStockTicker) {
//        return super.exportXls(request, stStockTicker, StStockTicker.class, "st_stock_ticker");
//    }
//
//    /**
//      * 通过excel导入数据
//    *
//    * @param request
//    * @param response
//    * @return
//    */
//    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
//    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
//        return super.importExcel(request, response, StStockTicker.class);
//    }

}
